package com.atguigu.eud_teacher.controller;

import java.util.*;

import com.atguigu.commonutils.R;
import com.atguigu.eud_teacher.entity.EduTeacherEntity;
import com.atguigu.eud_teacher.service.EduTeacherService;
import com.atguigu.eud_teacher.vo.TeachQuery;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;


/**
 * 讲师
 *
 * @author wuyao
 * @email 13918471632@163.com
 * @date 2021-03-13 19:42:07
 */
@RestController
@RequestMapping("/vue-admin-template/eduteacher")
@Api(tags = "讲师信息管理接口API")
@CrossOrigin
public class EduTeacherController {

    @Autowired
    EduTeacherService tService;

    //查询所有
    //throw报错，则后面的语句块不再执行
    //throws 表示这个方法可能抛出某种异常,但是程序会继续执行
    //     try {
    //         int a=10/0;
    //     }catch (Exception e){
    //        new DiyException(201,"0不能被整除，小学没学好？？");
    //         e.printStackTrace();
    //     }

    @ApiOperation(value = "所有所有讲师信息")
    @GetMapping("/getAllTeachers")
    public R getAllTeachers() {
        //根据创建时间进行降序排序
        List<EduTeacherEntity> eduTeacherEntityList = tService.list(new QueryWrapper<EduTeacherEntity>().orderByDesc("gmt_create"));
        return R.ok().data("list", eduTeacherEntityList);
    }

    //插件分页功能
    @ApiOperation(value = "分页查询所有讲师")
    //current当前页，limit每页几条数据，一共几条，records是这几条数据集合，详细数据信息
    @GetMapping("/pageTeachers/{current}/{limit}")
    public R pageTeachers(@PathVariable long current,
                          @PathVariable long limit) {
        Page<EduTeacherEntity> page = new Page<EduTeacherEntity>(current, limit);
        //创建完对象直接调用接口
        tService.page(page, null);
        //总记录数
        long total = page.getTotal();
        //显示数据集合，详细数据
        List<EduTeacherEntity> records = page.getRecords();
        return R.ok().data("total", total).data("records", records);
    }

    //根据对象条件查询
    @ApiOperation(value = "条件分页查询所有讲师")
    //current当前页，limit每页几条数据，一共几条，records是这几条数据集合，详细数据信息
    //请求参数直接写对象也可以，但是请求只能getmapping,如果加@requestBody就要postmapping请求
    //(required = false表示这个条件也可以不加
    @PostMapping("/wapperPage/{current}/{limit}")
    public R wapperPage(@PathVariable Long current,
                        @PathVariable Long limit,
                        @RequestBody(required = false) TeachQuery teacherQuery) {

        Page<EduTeacherEntity> pageTeacher = new Page<EduTeacherEntity>(current, limit);
        QueryWrapper<EduTeacherEntity> wrapper = new QueryWrapper<EduTeacherEntity>();
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        if (!StringUtils.isEmpty(level)) {
            wrapper.eq("level", level);
        }
        if (!StringUtils.isEmpty(name)) {
            wrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(begin)) {
            wrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            wrapper.le("gmt_modified", end);
        }
        wrapper.orderByDesc("gmt_create", begin);
        tService.page(pageTeacher, wrapper);
        //总记录数
        long total = pageTeacher.getTotal();
        //显示数据集合，详细数据
        List<EduTeacherEntity> records = pageTeacher.getRecords();
        HashMap<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("records", records);
        return R.ok().data(map);
    }


    //讲师逻辑删除功能
    @ApiOperation(value = "根据Id删除讲师")
    @DeleteMapping("/deleteId/{id}")
//    @ApiImplicitParam(name="id",value="id",required=true,paramType="query")
    public R deleteId(@PathVariable String id) {
        boolean flag = tService.removeById(id);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }

    //添加讲师功能
    @ApiOperation(value = "添加一条讲师")
    @PostMapping("/addTeacher")
    public R addTeacher(@RequestBody EduTeacherEntity teacherEntity) {
        Boolean flag = tService.save(teacherEntity);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }

    //根据id查询讲师
    @GetMapping("/getById/{id}")
    @ApiOperation(value = "通过id查询讲师")
    public R getById(@PathVariable String id) {
        EduTeacherEntity e = tService.getById(id);
        return R.ok().data("teacherById", e);
    }

    //修改讲师
    @PostMapping("/updateTeacher")
    @ApiOperation(value = "通过id修改讲师")
    public R updateTeacher(@RequestBody EduTeacherEntity e) {
        Boolean flag = tService.updateById(e);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }
}
